<!--页面主要内容-->
<main class="lyear-layout-content">
  
  <div class="container-fluid p-t-15">
	
	<div class="row">
	  <div class="col-lg-12">
		<div class="card">
		  <div class="card-toolbar d-flex flex-column flex-md-row">
			<div class="toolbar-btn-action">
			  <a class="btn btn-primary m-r-5" href="{:url('admin/index/register')}"><i class="mdi mdi-plus"></i> 新增</a>
			  <a class="btn btn-success m-r-5 ajax-post confirm" id="qidong" href="#!" target-form="ids"><i class="mdi mdi-check"></i> 启用</a>
			  <a class="btn btn-warning m-r-5" id="jinyong" href="#!"><i class="mdi mdi-block-helper"></i> 禁用</a>
			  <a class="btn btn-danger" id="del" href="#!"><i class="mdi mdi-window-close del"></i> 删除</a>
			</div>
			
			<form class="search-bar ml-md-auto" method="get" action="#!" role="form">
			  <input type="hidden" name="search_field" id="search-field" value="title" />
			  <div class="input-group ml-md-auto">
				<div class="input-group-prepend">
				  <button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" id="search-btn">标题</button>
				  <div class="dropdown-menu" style="">
					<a class="dropdown-item" href="#!" data-field="title">标题</a>
					<a class="dropdown-item" href="#!" data-field="cat_name">栏目</a>
				  </div>
				</div>
				<input type="text" class="form-control" name="keyword" placeholder="请输入名称">
			  </div>
			</form>
		  </div>
		  <div class="card-body">
			
			<div class="table-responsive">
			  <table class="table table-bordered">
				<thead>
				  <tr>
					<th>
					  <div class="custom-control custom-checkbox">
						<input type="checkbox" class="custom-control-input" id="check-all">
						<label class="custom-control-label" for="check-all"></label>
					  </div>
					</th>
					<th>管理员账号</th>
					<th>管理员昵称</th>
					<th>管理员状态</th>
					<th>操作</th>
				  </tr>
				</thead>
				<tbody>
				  {foreach $adminlist as $item}
					<tr>
					  <td>
					    <div class="custom-control custom-checkbox">
					  	<input type="checkbox" class="custom-control-input ids" name="ids[]" value="{$item.id}" id="{$item.id}">
					  	<label class="custom-control-label" for="{$item.id}"></label>
					    </div>
					  </td>
					  <td>{$item.username}</td>
					  <td>{$item.nickname}</td>
					  <td>
						  {if condition="$item.state == 1"}
							 <span class="badge badge-success">启用</span>
						  {else/}
							 <span class="badge badge-danger">禁用</span>
						  {/if}
					  </td>
					  <td>
						  
						<div class="btn-group">
												<a class="btn btn-xs btn-default" href="{:url('admin/index/upadmin',['id'=>$item.id])}" title="" data-toggle="tooltip" data-original-title="编辑"><i class="mdi mdi-pencil"></i></a>
												<a class="btn btn-xs btn-default ajax-get confirm deladmin" href="#!" data-id="{$item.id}" title="" data-toggle="tooltip" data-original-title="删除"><i class="mdi mdi-window-close"></i></a>
						</div>
					  </td>
					</tr>
				  {/foreach}
				  
				</tbody>
			  </table>
			</div>
			
			
			<!-- 分页 -->
			{$adminlist->render()}
			
		  </div>
		</div>
	  </div>
	
  </div>

</main>
<!--End 页面主要内容-->
<script type="text/javascript">
    
	
	function GetId() {
		// 存放id的数组
		let list = []
	
		$('input[name="ids[]"]:checked').each(function () {
			// console.log(item);
			list.push($(this).val())
		})
	
		return list
	}
	
    layui.use(['layer'],function(){
		layer = layui.layer
		
	// 删除
	$('.deladmin').click(function(){
		
		let id = $(this).data('id');
		
		$.ajax({
			type:'post',
			url:`{:url('admin/index/deladmin')}`,
			data:{
			  id
			},
			dataType:'json',
			success:function(res)
			{
			  if(res.code === 1)
			  {
				layer.msg(res.msg,{icon: 1},function(){
				  location.href = 'adminlist';
				})
			  }else{
				layer.msg(res.msg,{icon: 5})
			  }
			}
		})
		
	})

	
	// 启动
	$('#qidong').click(function(){
		
		let str = GetId().join(',');
		
		$.ajax({
			type:'post',
			url:`{:url('admin/index/qidong')}`,
			data:{
			  str
			},
			dataType:'json',
			success:function(res)
			{
			  if(res.code === 1)
			  {
				layer.msg(res.msg,{icon: 1},function(){
				  location.href = 'adminlist';
				})
			  }else{
				layer.msg(res.msg,{icon: 5})
			  }
			}
		})
		
	})
	
	
	// 禁用
	$('#jinyong').click(function(){
		
		let str = GetId().join(',');
		
		$.ajax({
			type:'post',
			url:`{:url('admin/index/jinyong')}`,
			data:{
			  str
			},
			dataType:'json',
			success:function(res)
			{
			  if(res.code === 1)
			  {
				layer.msg(res.msg,{icon: 1},function(){
				  location.href = 'adminlist';
				})
			  }else{
				layer.msg(res.msg,{icon: 5})
			  }
			}
		})
		
	})
	
	
	
	// 删除的数据
	let adminlist = [];
	// 删除指定
	let attstr = '';
	// 全部删除
	let action = '';
	
	// 删除
	$('#del').click(function(){
		
		$.ajax({
			type:'post',
			url:`{:url('admin/index/del')}`,
			data:{
			  attstr,
			  action
			},
			dataType:'json',
			success:function(res)
			{
			  if(res.code === 1)
			  {
				layer.msg(res.msg,{icon: 1},function(){
				  location.href = 'adminlist';
				})
			  }else{
				layer.msg(res.msg,{icon: 5})
			  }
			}
		})
		
	})
	
	// 全部删除
    $('#check-all').on('change',function(){
				
		if(this.checked){
			attstr = '';
			
			action = true;
			
		}else{
			
			action = false;
		}
		
	})
	
	
	
	$(document).on('change','.ids',function(){
		
		if(this.checked){
			
			adminlist.push($(this).val())
			
			attstr = adminlist.join(',')
			
		}else{
			
			adminlist.splice($.inArray($(this).val(), adminlist),1)
			
			attstr = adminlist.join(',')
			
		}
		
	})
	
	})
</script>